unit u_CorrigeStatus;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao_Rel, RDprint, ExtCtrls, StdCtrls, CheckLst, ComCtrls,
  Buttons, Mask, ToolEdit, dbtables;

type
  TfrmAtu_StatusComissao = class(TFrmPad_Rel)
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    edSeq: TEdit;
    edNota: TEdit;
    edData: TDateEdit;
    rgStatus: TRadioGroup;
    c_notaini: TSpeedButton;
    Label3: TLabel;
    edSer: TEdit;
    procedure FormActivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure bimprimirClick(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure edNotaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure c_notainiClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmAtu_StatusComissao: TfrmAtu_StatusComissao;

implementation

uses clsBackupVenda, Dm, Biblio, Cons_NotaFiscal;

var
  BackupVenda: TBackupVenda;
  nota1,
    nota2: string;

{$R *.DFM}

procedure TfrmAtu_StatusComissao.FormActivate(Sender: TObject);
begin
  inherited;
  edSeq.Clear;
  edNota.Clear;
  edData.Clear;
  edSeq.SetFocus;
end;

procedure TfrmAtu_StatusComissao.FormCreate(Sender: TObject);
begin
  inherited;
  BackupVenda := TBackupVenda.Create;
end;

procedure TfrmAtu_StatusComissao.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  BackupVenda.Free;
end;

procedure TfrmAtu_StatusComissao.bimprimirClick(Sender: TObject);
var
  tq: TQuery;
  Status: string;
begin
  inherited;
  BackupVenda.RecuperarNota(edSer.Text, edNota.Text, nota1, nota2);
  case rgStatus.ItemIndex of
    0: Status := 'P';
    1: Status := 'S';
    2: Status := 'N';
  end;

  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Update RepresMov set Repm_Liberado =:St ' +
    'where (Nota_Numero =:N1 or Nota_Numero =:N2) ' +
    'and Repm_Data =:Dt and Repm_Sequencia =:Seq ' +
    'and Rep_Codigo =:Rep');
  tq.ParamByName('St').AsString := Status;
  tq.ParamByName('N1').AsString := nota1;
  tq.ParamByName('N2').AsString := nota2;
  tq.ParamByName('Dt').AsDateTime := edData.Date;
  tq.ParamByName('Seq').AsInteger := StrToInt(Trim(edSeq.Text));
  tq.ParamByName('Rep').AsString := '003';
  try
    tq.ExecSQL;
  except
    ShowMessage('Erro ao atualizar status!');
  end;
  tq.Close;
  tq.Free;
  FormActivate(Sender);
end;

procedure TfrmAtu_StatusComissao.btncancelarClick(Sender: TObject);
begin
  inherited;
  FormActivate(Sender);
end;

procedure TfrmAtu_StatusComissao.edNotaKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key = 12 then
    edNota.Text := StrZero(edNota.Text, 9);
  if key = vk_f2 then c_notainiClick(Sender);
end;

procedure TfrmAtu_StatusComissao.c_notainiClick(Sender: TObject);
var
  ConsNotaVenda: TFrmCons_NotaFiscal;
begin
  inherited;
  ConsNotaVenda := TFrmCons_NotaFiscal.create(self);
  ConsNotaVenda.nftipo := 0;
  ConsNotaVenda.ShowModal;
  ednota.Text := ConsNotaVenda.nota;
  ConsNotaVenda.free;
end;

end.
